﻿using MassTransit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfTool.Models;

namespace WpfTool.XiTong
{
    /// <summary>
    /// Save.xaml 的交互逻辑
    /// </summary>
    public partial class Save : UserControl
    {
        public Guid Id { get; set; }
        public Save()
        {
            InitializeComponent();
        }

        public void Init()
        {
            if (Id != Guid.Empty)
            {
                WpfMenu entity = DbHelper.DbMssql.Queryable<WpfMenu>().First(m => m.Id == Id);
                MenuName.Text = entity.MenuName;
                MenuIcon.Text = entity.IconCode;
                MenuUrl.Text = entity.MenuUrl;
            }
        }

        private void SaveMenu_Click(object sender, RoutedEventArgs e)
        {
            WpfMenu model = new WpfMenu()
            {
                MenuName = MenuName.Text,
                IconCode = MenuIcon.Text,
                MenuUrl = MenuUrl.Text,
                ParentId = Guid.Empty,
                Id = NewId.NextGuid()
            };

            if (Id == Guid.Empty)
            {
                DbHelper.DbMssql.Insertable(model).ExecuteCommand();
            }
            else
            {
                model.Id = Id;
                DbHelper.DbMssql.Updateable(model).ExecuteCommand();
            }

            var aaa = Window.GetWindow(this);
            aaa.Close();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Init();
        }
    }
}
